package cn.xzhang.boot.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.xzhang.boot.common.exception.ServiceException;
import cn.xzhang.boot.common.pojo.PageResult;
import cn.xzhang.boot.mapper.WallpaperScoreMapper;
import cn.xzhang.boot.model.dto.wallpaperScore.WallpaperScoreAddReqDTO;
import cn.xzhang.boot.model.dto.wallpaperScore.WallpaperScorePageReqDTO;
import cn.xzhang.boot.model.dto.wallpaperScore.WallpaperScoreUpdateReqDTO;
import cn.xzhang.boot.model.entity.WallpaperScore;
import cn.xzhang.boot.model.vo.wallpaperScore.WallpaperScoreSimpleVo;
import cn.xzhang.boot.model.vo.wallpaperScore.WallpaperScoreVo;
import cn.xzhang.boot.service.WallpaperScoreService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

import static cn.xzhang.boot.common.exception.enums.GlobalErrorCodeConstants.*;
import static cn.xzhang.boot.common.exception.util.ServiceExceptionUtil.exception;

/**
 * 针对表【wallpaperScore(壁纸评分表)】的数据库操作Service实现
 *
 * @author <a href="https://github.com/XiaoZhangCode">XiaoZhangCode</a>
 */
@Service
public class WallpaperScoreServiceImpl extends ServiceImpl<WallpaperScoreMapper, WallpaperScore> implements WallpaperScoreService {

    @Resource
    private WallpaperScoreMapper wallpaperScoreMapper;

    /**
     * 添加新壁纸评分
     *
     * @param wallpaperScoreReqDTO 壁纸评分信息请求DTO
     * @return 添加成功返回壁纸评分id
     */
    @Override
    public long addWallpaperScore(WallpaperScoreAddReqDTO wallpaperScoreReqDTO) {
        WallpaperScore wallpaperScore = new WallpaperScore();
        BeanUtil.copyProperties(wallpaperScoreReqDTO, wallpaperScore);
        if (!this.save(wallpaperScore)) {
            throw exception(ADD_FAIL);
        }
        return wallpaperScore.getId();
    }

    /**
     * 更新壁纸评分信息
     *
     * @param wallpaperScoreReqDTO 壁纸评分信息更新请求DTO
     * @return 更新成功返回true
     */
    @Override
    public boolean updateWallpaperScore(WallpaperScoreUpdateReqDTO wallpaperScoreReqDTO) {
        if (wallpaperScoreReqDTO.getId() == null) {
            throw exception(BAD_REQUEST);
        }
        WallpaperScore wallpaperScore = new WallpaperScore();
        BeanUtil.copyProperties(wallpaperScoreReqDTO, wallpaperScore);
        boolean b = this.updateById(wallpaperScore);
        if (!b) {
            throw exception(UPDATE_FAIL);
        }
        return true;
    }

    /**
     * 删除壁纸评分
     *
     * @param id 壁纸评分id
     * @return 删除成功返回true
     */
    @Override
    @Transactional(rollbackFor = ServiceException.class)
    public boolean deleteWallpaperScore(Long id) {
        if (id == null) {
            throw exception(BAD_REQUEST);
        }
        boolean b = this.removeById(id);
        if (!b) {
            throw exception(DELETE_FAIL);
        }
        return true;
    }

    /**
     * 将壁纸评分对象转换为壁纸评分VO对象
     *
     * @param wallpaperScore 壁纸评分对象
     * @return 返回壁纸评分VO对象
     */
    @Override
    public WallpaperScoreSimpleVo getSimpleWallpaperScoreVO(WallpaperScore wallpaperScore) {
        if (wallpaperScore == null) {
            return null;
        }
        WallpaperScoreSimpleVo wallpaperScoreSimpleVo = new WallpaperScoreSimpleVo();
        BeanUtil.copyProperties(wallpaperScore, wallpaperScoreSimpleVo);
        return wallpaperScoreSimpleVo;
    }

    /**
     * 获取壁纸评分分页信息
     *
     * @param wallpaperScorePageReqDTO 壁纸评分分页请求DTO
     * @return 返回壁纸评分分页结果
     */
    @Override
    public PageResult<WallpaperScoreVo> getWallpaperScorePage(WallpaperScorePageReqDTO wallpaperScorePageReqDTO) {
        PageResult<WallpaperScore> pageResult = wallpaperScoreMapper.selectPage(wallpaperScorePageReqDTO);
        if (pageResult.getList() == null) {
            return PageResult.empty();
        }
        List<WallpaperScoreVo> wallpaperScoreVos = pageResult.getList().stream().map(wallpaperScore -> {
            WallpaperScoreVo wallpaperScoreVo = new WallpaperScoreVo();
            BeanUtil.copyProperties(wallpaperScore, wallpaperScoreVo);
            return wallpaperScoreVo;
        }).collect(Collectors.toList());
        return new PageResult<>(wallpaperScoreVos, pageResult.getTotal());
    }

    @Override
    public WallpaperScoreVo getWallpaperScoreVO(WallpaperScore wallpaperScore) {
        if (wallpaperScore == null) {
            return null;
        }
        WallpaperScoreVo wallpaperScoreVo = new WallpaperScoreVo();
        BeanUtil.copyProperties(wallpaperScore, wallpaperScoreVo);
        return wallpaperScoreVo;
    }

}



